#include "link.h"
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include "mqtt.h"
using namespace std;
extern  LINKnode* head;
void createhead()
{

 head=( LINKnode*)malloc(sizeof( LINKnode));
 if(head==NULL)
 {
    cout<<"malloc fail"<<endl;

 }
 head->next=NULL;
}
void add_link(int fd,const char*topic)
{

  LINKnode*p=head;
  while(p->next!=NULL)
  {
   p=p->next;

  }
  LINKnode* newnode=(LINKnode*)malloc(sizeof(LINKnode));
  newnode->fd=fd;
  strcpy(newnode->topic,topic);
  newnode->next=NULL;
  p->next=newnode;

}
void real_publish(const char*topic,const char*msg,int msglen)
{

   LINKnode*p=head->next;
   while(p)
   {
      if(!strcmp(p->topic,topic))
        {

          notify(p->fd,topic,msg,msglen);

        }



     p=p->next;





   }










}

void delete_link(int fd)
{
 
   LINKnode*p=head;
   while(p->next)
   {
       if(p->next->fd==fd)
       {
            LINKnode*deletenode=p->next;
            p->next=deletenode->next;
            free(deletenode);
       }
       else
       {

       p=p->next;


       }




   }







}